<?php
namespace app\controller;

use app\BaseController;
use app\controller\model\Tables;
use liliuwei\think\Auth;
use EasyDingTalk\Application;
use think\facade\Session;
use think\facade\Db;
class Includes extends BaseController
{
    protected $getpath="";
    protected $gobaluser="";
    protected $tables;
    public function __construct(){
        checkuser();
        $this->gobaluser=Session::get("uid");
        $this->tables = new Tables();
    }
    public function supplierList(){
        $key = request()->param('key');
        $res = DB::name($this->tables->supplier)->field("id,id as numbers,title,'' as sortTitle")->where("status",0)->where("title","like","%$key%")->select();
        echojson($res);
    }
    public function selects(){
        $id = request()->param("id");
        $path = request()->param("path");
        return view()->assign(['id'=>$id,'path'=>$path]);
    }
    public function selects2(){
        $id = request()->param("id");
        if($id>0){
            $res = r_classes($id);
            echojson($res);
        }
        echojson([],1,'id错误');
    }
    public function getInfo(){
        $table=request()->param("table");
        $id=request()->param("id");
        $inArr=['warehouse_product'];
        $reArr=[];
        if(in_array($table,$inArr)){
            if($table=='warehouse_product'){
                $re = r_one($table,"id=$id and status=0");
                if($re){
                    $reArr['numbers']=$re['numbers'];
                    $reArr['title']=$re['title'];
                    $reArr['sortTitle']=$re['sortTitle'];
                    $reArr['unit']=rClassIdToTitle($re['unit']);
                    $reArr['parameter']=$re['parameter'];
                    $reArr['unit2']=rClassIdToTitle($re['unit2']);
                    $reArr['volumeOut']=$re['volumeOut'];
                    $reArr['area']=$re['area'];
                }
            }
        }
        echojson($reArr);
    }
    public function upload(){
        //开始上传
        $arr=[];
        $file = request() -> file('file');
        $t = request() -> param('t');
        if(!isset($_FILES["file"])){echojson([],1,"上传失败");}
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if(!in_array(strtolower($extension), array("jpg","jpeg","png","xls","xlsx","doc","docx","ppt","pptx","pdf","mp4"))){
            echojson([],1,"不合法后缀");
        }

        $saveName = \think\facade\Filesystem::disk('photo') -> putFile('photo', $file, 'md5');
        $path=str_replace('\\', '/', '/uploads/'.date("Ym").'/' . $saveName);
//        return $path;
        echojson(['src'=>$path]);
        //上传结束
    }

    protected function smallImg($img){
        if(strpos($img,"mp4")>0){
            return true;
        }
        list($width, $height) = getimagesize(".".$img);
        if($width>1000){
            $percent = 1000 / $width;
        }else{
            $percent = 1 ;
        }

        $newwidth = $width * $percent;
        $newheight = $height * $percent;
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $image_info = getimagesize(".".$img);
        switch ($image_info['mime']) {
            case 'image/png':
                // 处理 PNG 文件
                $source = imagecreatefrompng('.'.$img);
                break;
            case 'image/jpeg':
                // 处理 JPEG 文件
                $source = imagecreatefromjpeg('.'.$img);
                break;
            case 'image/gif':
                // 处理 GIF 文件
                $source = imagecreatefromgif('.'.$img);
                break;
            default:
                // 其他类型的文件   不压缩直接跳过
                return false;
        }

        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($thumb,'.'.$img);
    }
}
